package com.headwind.logistics.logistics.service.impl;

import com.headwind.logistics.common.pojo.Coupon;
import com.headwind.logistics.logistics.dto.LogisticsIntegralTypeDTO;
import com.headwind.logistics.logistics.mapper.LogisticsCustomerFortuneMapper;
import com.headwind.logistics.logistics.pojo.LogisticsIntegral;
import com.headwind.logistics.logistics.pojo.LogisticsIntegralMall;
import com.headwind.logistics.logistics.pojo.LogisticsIntegralMallDetail;
import com.headwind.logistics.logistics.service.intl.LogisticsIntegralServiceIntl;
import com.headwind.logistics.system.pojo.IntegralMall;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @Author 小恶魔
 * @Date 2020-08-02 13:16
 * @Version 1.0
 * @DESCRIPTION 前台积分业务逻辑层
 */
@Service
public class LogisticsIntegralServiceImpl implements LogisticsIntegralServiceIntl {

    @Resource
    private LogisticsCustomerFortuneMapper logisticsCustomerFortuneMapper;

    /**
     * 根据顾客获取积分详情
     * @param customerId
     * @return
     */
    @Override
    public List<LogisticsIntegralTypeDTO> queryIntgralDetailByCustomerId(Long customerId) {
        return logisticsCustomerFortuneMapper.queryIntgralDetailByCustomerId(customerId);
    }

    /**
     * 获取全部积分商城的商品
     * @return
     */
    @Override
    public List<LogisticsIntegralMall> queryAllIntegralMall() {
        return logisticsCustomerFortuneMapper.queryAllIntegralMall();
    }

    /**
     * 积分商城兑换
     * @param logisticsIntegral
     * @param integralMall
     * @param customerId
     * @return
     */
    @Override
    @Transactional
    public Integer integralExchange(LogisticsIntegral logisticsIntegral, LogisticsIntegralMall integralMall, Long customerId) {
        //给积分记录对象赋值 客户id
        logisticsIntegral.setCustomerId(customerId);

        //创建积分商城明细实体类
        LogisticsIntegralMallDetail logisticsIntegralMallDetail = new LogisticsIntegralMallDetail();
        logisticsIntegralMallDetail.setIntegralMallId(integralMall.getIntegralMallId());
        logisticsIntegralMallDetail.setCustomerId(customerId);
        //添加积分商城明细表
        logisticsCustomerFortuneMapper.addIntegralMallDetail(logisticsIntegralMallDetail);
        //添加积分记录表
        logisticsCustomerFortuneMapper.addIntegral(logisticsIntegral);
        //创建优惠卷实体类
        Coupon coupon = new Coupon();
        coupon.setName(integralMall.getIntegralMallName());
        //获取6个月后的时间
        GregorianCalendar now = new GregorianCalendar();
        SimpleDateFormat fmtrq = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat df = DateFormat.getDateInstance();

        now.add(GregorianCalendar.MONTH,9);   //可以是天数或月数  数字自定 -6前6个月

        String str=fmtrq.format(now.getTime());  //9个月后时间
        coupon.setExpire(str);
        coupon.setVerify(1);
        coupon.setDiscount(Math.toIntExact(integralMall.getMoney()));
        coupon.setFullSubtract(10L);
        coupon.setCustomerId(customerId);
        //添加优惠卷
        int count = logisticsCustomerFortuneMapper.addCoupon(coupon);

        if (count >0){
            //修改客户的积分信息
            logisticsCustomerFortuneMapper.updateCustomerIntegralById(Math.toIntExact(integralMall.getIntegralPrice()),customerId);

        }

        return count;
    }
}
